package service

import (
	"workflow/config"
	"workflow/entity"
)

func ListHistory(workflowID string) (history []entity.History, err error) {
	err = config.DB.Where("workflow_id = ?", workflowID).Order("created_at desc").Find(&history).Error
	if err != nil {
		return nil, err
	}
	return history, nil
}

func FindHistoryDetail(historyID string) (history *entity.History, err error) {
	err = config.DB.Where("id = ?", historyID).First(&history).Error
	if err != nil {
		return nil, err
	}

	nodes := make([]entity.HistoryNode, 0)
	err = config.DB.Where("history_id = ?", history.ID).Find(&nodes).Error
	if err != nil {
		return nil, err
	}
	history.Nodes = nodes

	var trigger entity.HisotryTrigger
	err = config.DB.Where("id = ?", history.TriggerID).First(&trigger).Error
	if err != nil {
		return nil, err
	}
	history.Trigger = trigger
	return history, nil
}
